package com.ruoyi.system.service.impl;

import java.util.Collection;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProspectsStudentMapper;
import com.ruoyi.system.domain.ProspectsStudent;
import com.ruoyi.system.service.IProspectsStudentService;

/**
 * 潜在学员Service业务层处理
 * 
 * @author tong
 * @date 2024-01-23
 */
@Service
public class ProspectsStudentServiceImpl implements IProspectsStudentService 
{
    @Autowired
    private ProspectsStudentMapper prospectsStudentMapper;

    /**
     * 查询潜在学员
     * 
     * @param id 潜在学员主键
     * @return 潜在学员
     */
    @Override
    public ProspectsStudent selectProspectsStudentById(Long id)
    {
        return prospectsStudentMapper.selectProspectsStudentById(id);
    }

    /**
     * 查询潜在学员列表
     * 
     * @param prospectsStudent 潜在学员
     * @return 潜在学员
     */
    @Override
    public List<ProspectsStudent> selectProspectsStudentList(ProspectsStudent prospectsStudent)
    {
        prospectsStudent.setConsultTeacher(SecurityUtils.getUsername());
        List<ProspectsStudent> prospectsStudents = prospectsStudentMapper.selectProspectsStudentList(prospectsStudent);
        List<ProspectsStudent> anotherProspectsStudents = prospectsStudentMapper.selectAllNotAbout();
        Collection<ProspectsStudent> union = CollUtil.union(prospectsStudents, anotherProspectsStudents);
        return union.stream().toList();
    }

    /**
     * 新增潜在学员
     * 
     * @param prospectsStudent 潜在学员
     * @return 结果
     */
    @Override
    public int insertProspectsStudent(ProspectsStudent prospectsStudent)
    {
        return prospectsStudentMapper.insertProspectsStudent(prospectsStudent);
    }

    /**
     * 修改潜在学员
     * 
     * @param prospectsStudent 潜在学员
     * @return 结果
     */
    @Override
    public int updateProspectsStudent(ProspectsStudent prospectsStudent)
    {
        return prospectsStudentMapper.updateProspectsStudent(prospectsStudent);
    }

    /**
     * 批量删除潜在学员
     * 
     * @param ids 需要删除的潜在学员主键
     * @return 结果
     */
    @Override
    public int deleteProspectsStudentByIds(Long[] ids)
    {
        return prospectsStudentMapper.deleteProspectsStudentByIds(ids);
    }

    /**
     * 删除潜在学员信息
     * 
     * @param id 潜在学员主键
     * @return 结果
     */
    @Override
    public int deleteProspectsStudentById(Long id)
    {
        return prospectsStudentMapper.deleteProspectsStudentById(id);
    }

    /**
     * 接管潜在学员
     * @param id
     */
    @Override
    public void takeOverStudent(Long id) {
        //1.业务校验
        ProspectsStudent prospectsStudent = prospectsStudentMapper.selectProspectsStudentById(id);
        Assert.isTrue(prospectsStudent != null,"没有该潜在学员");
        String consultTeacher = prospectsStudent.getConsultTeacher();
        Assert.isTrue(consultTeacher == null || consultTeacher == "","该潜在学员已经有咨询老师");

        prospectsStudent.setConsultTeacher(SecurityUtils.getUsername());
        int result = prospectsStudentMapper.updateProspectsStudent(prospectsStudent);
        Assert.isTrue(result > 0,"接管潜在学员失败");
    }

    /**
     * 放弃潜在学员
     * @param id
     */
    @Override
    public void abandonStudent(Long id) {
        ProspectsStudent prospectsStudent = prospectsStudentMapper.selectProspectsStudentById(id);
        Assert.isTrue(prospectsStudent != null,"没有该潜在学员");
        Assert.isTrue(prospectsStudent.getConsultTeacher().equals(SecurityUtils.getUsername())  ,"该潜在学员不属于你");
        prospectsStudent.setConsultTeacher("");
        int result = prospectsStudentMapper.updateProspectsStudent(prospectsStudent);
        Assert.isTrue(result > 0,"放弃潜在学员失败");
    }
}
